<%@ page import="java.util.ArrayList"%>
<%@ page import="domain.Contact"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>
<%@ taglib prefix="nested" uri="http://struts.apache.org/tags-nested"%>
<% ArrayList<Contact> contacts = (ArrayList<Contact>) request.getAttribute("contacts");
   String type = (String) request.getAttribute("type");
   String key = type.equals("update")?"updateContact.title":(type.equals("delete")?"deleteContact.title":"showContacts.title");
   Boolean contactAdded = (Boolean) request.getAttribute("contactAdded");
   Boolean contactUpdated = (Boolean) request.getAttribute("contactUpdated");
   Boolean contactDeleted = (Boolean) request.getAttribute("contactDeleted");%>
<!DOCTYPE html>
<html:html>
<head>
<title><bean:message key="<%=key%>" /></title>
<jsp:include page="/pages/header.jsp" />
</head>
<body>
	<div class="container">
		<div class="row">
			<div class="span12">
				<div class="page-header">
					<h1>
						<bean:message key="<%=key%>" />
					</h1>
				</div>
			</div>
		</div>
		<div class="row">
			<jsp:include page="/pages/navbar.jsp" />
			<%if(contactAdded != null){
				if(contactAdded){
					%><div class="alert alert-block span9 alert-success">
    		<html:button property="button" styleClass="close" value="×"/>
        	<h4><bean:message key="contactAdded" /></h4>
    		</div><%			
				} else {
					%><div class="alert alert-block span9 alert-error">
		    		<html:button property="button" styleClass="close" value="×"/>
		        	<h4><bean:message key="contactNotAdded" /></h4>
		    		</div><%					
				}
			}%>			
			<%if(contactUpdated != null){
				if(contactUpdated){
					%><div class="alert alert-block span9 alert-success">
    		<html:button property="button" styleClass="close" value="×"/>
        	<h4><bean:message key="contactUpdated" /></h4>
    		</div><%			
				} else {
					%><div class="alert alert-block span9 alert-error">
		    		<html:button property="button" styleClass="close" value="×"/>
		        	<h4><bean:message key="contactNotUpdated" /></h4>
		    		</div><%					
				}
			}%>	
			<%if(contactDeleted != null){
				if(contactDeleted){
					%><div class="alert alert-block span9 alert-success">
    		<html:button property="button" styleClass="close" value="×"/>
        	<h4><bean:message key="contactDeleted" /></h4>
    		</div><%			
				} else {
					%><div class="alert alert-block span9 alert-error">
		    		<html:button property="button" styleClass="close" value="×"/>
		        	<h4><bean:message key="contactNotDeleted" /></h4>
		    		</div><%					
				}
			}%>	
			<div class="span10">
				<table class="table table-hover">
			<tr class="bandeau"><th>Id</th><th>Prénom</th><th>Nom</th><th>Email</th><th></th></tr>
			<% 	if(contacts != null){
					for(int i=0; i < contacts.size();i++){
					String formId = "form-".concat(String.valueOf(contacts.get(i).getId()));
					%> 
					<tr>
						<td><%= contacts.get(i).getId()%></td> 
						<td><%= contacts.get(i).getFirstName()%></td>
						<td><%= contacts.get(i).getLastName()%></td>
						<td><%= contacts.get(i).getEmail()%></td>
						<% 	if(type.equals("update")){%>
						<td>
							<html:form styleClass="updateForm" action="/updateContactLink">
							<html:hidden property="id" value="<%=String.valueOf(contacts.get(i).getId())%>"/>
							<button type="submit" name="btnUpdateSimple" class="btn btn-small btn-primary"><i class="icon-white icon-pencil"></i>&nbsp;<bean:message key="updateContactButton" /></button>
							</html:form>
						</td>
						<% }else if(type.equals("delete")){%>
						<td>
							<html:form styleClass="deleteForm" styleId="<%=formId%>" action="/deleteContact">
							<html:hidden property="id" value="<%=String.valueOf(contacts.get(i).getId())%>"/>
							<button type="submit" name="btnDeleteSimple" id="<%=String.valueOf(contacts.get(i).getId())%>" class="btnDeleteSimple btn btn-small btn-primary"><i class="icon-white icon-remove-sign"></i>&nbsp;<bean:message key="deleteContactButton" /></button>
							</html:form>
						</td>		
						<%} else {%>
						<td>
							<html:form styleClass="showForm" action="/showContact">
							<html:hidden property="id" value="<%=String.valueOf(contacts.get(i).getId())%>"/>
							<button type="submit" name="btnShowSimple" class="btnShowSimple btn btn-small btn-primary"><i class="icon-white icon-user"></i>&nbsp;<bean:message key="showContactButton" /></button>
							</html:form>
						</td>		
						<%}%>
					</tr>
				<%	} }%>
		</table>
			</div>
		</div>
		<jsp:include page="/pages/footer.jsp" />
	</div>
</body>
</html:html>